#ifndef IMAGEINSTANCEDATA_H
#define IMAGEINSTANCEDATA_H

#include <QSharedData>
#include <QString>
#include <QPixmap>
#include <QDateTime>
#include "dcmtk/dcmimgle/diutils.h"
class DcmTagKey;
class DicomImage;
class DcmFileFormat;

class ImageInstanceData: public QSharedData
{
public:
    explicit ImageInstanceData(const QString &file);
    explicit ImageInstanceData(DcmFileFormat *dff);
    ~ImageInstanceData();

    void setWindow(const double &center, const double &width)
    {
        winCenter = center; winWidth = width;
    }

    void getWindow(double &center, double &width) const
    {
        center = winCenter; width = winWidth;
    }

    void setWindowDelta(const double &dCenter, const double &dWidth)
    {
        winCenter += dCenter; winWidth += dWidth;
    }

    void setRoiWindow(const QRectF &rect);
    void setFullDynamic();
    void setDefaultWindow()
    {
        winCenter = defCenter; winWidth = defWidth;
    }

    QString getStudyUid() const { return studyUid; }
    QString getSeriesUid() const { return seriesUid; }
    QString getImageUid() const { return imageUid; }
    QString getClassUid() const { return classUid; }
    QString getImageFile() const { return imageFile; }

    void setPolarity(EP_Polarity polarity);
    EP_Polarity getPolarity() const;
    bool getPixmap(QPixmap &pixmap);
    static bool getPixmap(const QString &dicomFile, QPixmap &pixmap);
    static bool dcm2bmpHelper(DicomImage &dcmImage, QPixmap &pixmap);
    static void freeBuffer(void *pBuf) { delete pBuf; }

    inline bool isNormal() const;
    DicomImage* createClippedImage(const QRect &rect, int angle = 0, bool hflip = false, bool vflip = false, bool inverted = false);
    QString getTagKeyValue(const DcmTagKey &key) const;
    double getPixelValue(long x, long y) const;
    bool getPixSpacing(double &spacingX, double &spacingY) const;
    bool getImageSize(ulong &width, ulong &height) const;
    const short *getInternalPtr() const;
    const ushort *getRawData() const;

    const DicomImage *getDcmImage() const;
    DcmFileFormat *getFileFormat() const;
    bool saveFileFormat();

private:
    void initImage();

    QString studyUid;
    QString seriesUid;
    QString imageUid;
    QString classUid;
    double pixelX;
    double pixelY;
    double defCenter;
    double defWidth;
    double winWidth;
    double winCenter;
    DcmFileFormat *dcmff;
    DicomImage *dcmImage;
    QString imageFile;
};

#endif // IMAGEINSTANCEDATA_H
